package com.androidhive.xmlparsing;

import java.io.IOException;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class XMLParser {
	
	// private static final String SOAP_ACTION = "http://service.kacanbalikbuyukolur.com/test/HelloWorld";
	 //   private static final String METHOD_NAME = "HelloWorld";
	 //   private static final String NAMESPACE = "http://service.kacanbalikbuyukolur.com/test/";
	 //   private static final String URL = "http://service.kacanbalikbuyukolur.com/test/getdeals.asmx";
	// constructor
	public XMLParser() {

	}

	/**
	 * Getting XML from URL making HTTP request
	 * @param url string
	 * */
	
	
	/**
	 * Getting XML DOM element
	 * @param XML string
	 * */
	public Document getDomElement(String city, int PageIndex){
		Document doc = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			
			
		
	       
	            	         
	            // request.addProperty("CityID", city);
//	            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
//                        SoapEnvelope.VER11);
//                envelope.dotNet = true;
//                envelope.setOutputSoapObject(request);
//
//                AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
//
//                SoapPrimitive result = null;
//                try {
//                    httpTransport.call(SOAP_ACTION, envelope);
//                    result = (SoapPrimitive) envelope.getResponse();
//             
//                } catch (IOException e) {
//                    // TODO Auto-generated catch block
//                	e.printStackTrace();
//                } catch (XmlPullParserException e) {
//                    // TODO Auto-generated catch block
//                	e.printStackTrace();
//                }


	       

			 
			DocumentBuilder db = dbf.newDocumentBuilder();
			URL url=new URL("http://service.kacanbalikbuyukolur.com/test/xmldeals.aspx?cid=" + city+ "&index="+PageIndex);

		        doc = db.parse(new InputSource(url.openStream())); 

			} catch (ParserConfigurationException e) {
				Log.e("Error: ", e.getMessage());
				return null;
			} catch (SAXException e) {
				Log.e("Error: ", e.getMessage());
	            return null;
			} catch (IOException e) {
				Log.e("Error: ", e.getMessage());
				return null;
			}

	        return doc;
	}
	
	/** Getting node value
	  * @param elem element
	  */
	 public final String getElementValue( Node elem ) {
	     Node child;
	     if( elem != null){
	         if (elem.hasChildNodes()){
	             for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
	                 if( child.getNodeType() == Node.TEXT_NODE  ){
	                     return child.getNodeValue();
	                 }
	             }
	         }
	     }
	     return "";
	 }
	 
	 /**
	  * Getting node value
	  * @param Element node
	  * @param key string
	  * */
	 public String getValue(Element item, String str) {		
			NodeList n = item.getElementsByTagName(str);		
			return this.getElementValue(n.item(0));
		}
}
